In [1]:
from openanalysis.matrix_animator import MatrixAnimator
import numpy as np # Needed to work with arrays
networkx
graphdef algorithm_name(G):
import networkx as nx
M = nx.to_numpy_matrix(G)
# do other work now
m, n = M.shape
for i in range(0, n):
for j in range(0, n):
if i != j and D[i, j] == 0:
M[i, j] = float('inf')
yield
copy of current version of matrix, along with a tuple containing current 3 co-ordinates at which change is causedyield np.array(D), (i, j, k)
Now, Let's implement the algorithm
In [2]:
def Floyd_Warshall(G): # Must have signature like this
D = nx.to_numpy_matrix(G) # Obtaining Adj. matrix
m, n = D.shape
for i in range(0, n): # Making non-diagonal zeros to infinity, as it is a Weighted Graph
for j in range(0, n):
if i != j and D[i, j] == 0:
D[i, j] = float('inf')
yield np.array(D), (0, 0, 0) # Starting yield
count = 0
for k in range(0, n):
for i in range(0, n):
for j in range(0, n):
if D[i, j] > D[i, k] + D[k, j]:
yield np.array(D), (i, j, k) # yield as array changes
D[i, j] = D[i, k] + D[k, j]
count += 1
yield np.array(D), (0, 0, 0)
MatrixAnimator
class__init__(self, fn, G):
fn
: A function yielding matrix along with 3-tupleG
: Graph on which fn
has to be applied and visualizedanimate(self, save=False):
save
is True
implies animation is saved in output/
folderapply_to_graph(self, show_graph=True):
self.fn
to self.G
and displays the resultshow_graph
is True
implies Graph is shown along with adjacancy matrix and final matrixHere we shall create a matrix from numpy
array, and assign random weights to its edges. Then we apply our function to graph
In [3]:
import networkx as nx
M = nx.from_numpy_matrix(
np.matrix(
[[0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0]]
))
import random
for u, v in M.edges():
M.edge[u][v]['weight'] = random.randint(1, 10)
animator = MatrixAnimator(Floyd_Warshall, M)
animator.apply_to_graph()
After executing
animator.animate(save=True)
go to output/
directory to see the mp4
files
You can see more examples at Github